<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-08-17 16:10:31
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-12 16:46:38
-->
<template>
  <div>
    <el-drawer
      title="详细信息"
      :visible.sync="dialog.ghDrawer"
      :direction="direction"
      :with-header="false"
      size="60%"
      :modal="false"
      @close="drawerClose"
    >
      <div class="head">
        <div class="header">
          <div class="img">
            <img src="@/assets/images/icon-batch.png" alt="" />
            <h3>详细信息</h3>
          </div>
          <div class="btn">
            <el-button
              type="primary"
              @click="receive"
              v-hasPermi="['serviceProvide:customer:receive']"
            >
              领取
            </el-button>
            <el-button
              type="danger"
              plain
              @click="distributionGh"
              v-hasPermi="['serviceProvide:customer:distribution']"
            >
              分配
            </el-button>
            <el-dropdown style="margin-left: 10px">
              <el-button class="el-icon-more"></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item> 导入 </el-dropdown-item>
                <el-dropdown-item> 导出 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="footer">
          <div class="information">
            <span>线索名称</span>
            <h4>{{ drawerData.name }}</h4>
          </div>
          <div class="information">
            <span>客户级别</span>
            <h4>{{ drawerData.levelName }}</h4>
          </div>
          <div class="information">
            <span>成交状态</span>
            <h4>{{ drawerData.statusName }}</h4>
          </div>
          <div class="information">
            <span>负责人</span>
            <h4>{{ drawerData.directorName }}</h4>
          </div>
        </div>
      </div>
      <div class="strip"></div>
      <div class="specific">
        <el-tabs v-model="activeName" type="border-card">
          <el-tab-pane label="活动" name="first">
            <gh-activity
              class="activ"
              :drawerData="drawerData"
              :furtherData="furtherData"
              :dialog="dialog"
              :getfurther="getfurther"
              :activityForm="activityForm"
              :getEnclosure="getEnclosure"
            ></gh-activity>
          </el-tab-pane>
          <el-tab-pane label="详细资料" name="second">
            <div class="basics">基础信息</div>
            <div class="basics-information">
              <div class="subclass">
                <span>客户名称：{{ drawerData.name }}</span>
              </div>
              <div class="subclass">
                <span>手机号码：{{ drawerData.phone }}</span>
              </div>
              <div class="subclass">
                <span>客户行业：{{ drawerData.sourceName }}</span>
              </div>
              <div class="subclass">
                <span>线索来源：{{ drawerData.sourceName }}</span>
              </div>
              <div class="subclass">
                <span>客户级别：{{ drawerData.levelName }}</span>
              </div>
              <div class="subclass">
                <span>备注：{{ drawerData.remak }} </span>
              </div>
            </div>
            <div class="system">地理信息</div>
            <div class="basics-information">
              <div class="subclass">
                <span>地区：{{ drawerData.regionName }}</span>
              </div>
              <div class="subclass">
                <span>地址：{{ drawerData.address }} </span>
              </div>
            </div>
            <div class="system">系统信息</div>
            <div class="basics-information">
              <div class="subclass">
                <span>创建人：{{ drawerData.createBy }}</span>
              </div>
              <div class="subclass">
                <span>负责人：{{ drawerData.directorName }}</span>
              </div>
              <div class="subclass">
                <span>创建时间：{{ drawerData.createTime }}</span>
              </div>
              <div class="subclass">
                <span>更新时间：{{ drawerData.updateTime }}</span>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="联系人" name="third">
            <el-button
              type="primary"
              size="default"
              @click="newContact"
              v-hasPermi="['serviceProvide:customer:addHighSeasContact']"
            >
              新建联系人
            </el-button>
            <el-table :data="contactData" height="3.3rem">
              <template slot="empty" class="emptyBg">
                <img src="@/assets/images/empty.jpg" alt="" />
              </template>
              <el-table-column label="姓名" prop="name"> </el-table-column>
              <el-table-column label="手机号" prop="phone"> </el-table-column>
              <el-table-column label="职务" prop="post"> </el-table-column>
              <el-table-column label="是否关键决策人">
                <template slot-scope="scope">
                  {{ scope.row.leader == 1 ? "是" : "否" }}
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="商机" name="fourth">
            <el-button
              type="primary"
              size="default"
              @click="newOpportunity"
              v-hasPermi="['serviceProvide:customer:addHighSeasOpportunity']"
            >
              新建商机
            </el-button>
            <el-table :data="opportunityData">
              <el-table-column label="商机名称" prop="name"> </el-table-column>
              <el-table-column label="商机金额" prop="amount">
              </el-table-column>
              <el-table-column label="客户名称" prop="name"> </el-table-column>
              <el-table-column label="商机状态组" prop="status">
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="附件" name="fifth">
            <el-upload
              class="upload-demo"
              ref="uploadFile"
              action="#"
              accept=".xlsx,.xls,.doc,.docx,.pdf"
              :file-list="fileList"
              :on-change="getFile1"
              :auto-upload="false"
              :limit="1"
              :on-remove="handleRemove1"
              style="margin-top: 10px"
            >
              <el-button
                class="taskBtn"
                v-hasPermi="['serviceProvide:customer:importHighSeasEnclosure']"
              >
                上传附件
              </el-button>
            </el-upload>
            <el-table :data="enclosureData" height="3.3rem">
              <el-table-column label="附件名称" prop="fileName">
              </el-table-column>
              <el-table-column label="附件大小" prop="size"> </el-table-column>
              <el-table-column label="上传人" prop="createBy">
              </el-table-column>
              <el-table-column label="上传时间" prop="createTime">
              </el-table-column>
              <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                  <span
                    class="cz"
                    @click="preview(scope.row)"
                    v-hasPermi="['serviceProvide:customer:previewHighSeas']"
                    >预览</span
                  >
                  <a :href="scope.row.fileUrl" class="cz">下载</a>
                  <span
                    class="cz"
                    @click="renameCus(scope.row)"
                    v-hasPermi="['serviceProvide:customer:renameHighSeas']"
                    >重命名</span
                  >
                  <span
                    class="del"
                    @click="del(scope.row)"
                    v-hasPermi="[
                      'serviceProvide:customer:delHighSeasEnclosure',
                    ]"
                    >删除</span
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="操作记录" name="sixth">
            <div
              class="operation"
              v-for="(item, index) in Operation"
              :key="index"
            >
              <div class="user">
                <div class="portrait">
                  <img :src="item.avatar" alt="" />
                </div>
                <div class="name">{{ item.createBy }}</div>
              </div>
              <div class="record">
                <div class="time">{{ item.createTime }}</div>
                <div class="content">
                  <div>{{ item.content }}</div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
    <el-dialog
      title="新建商机"
      :visible.sync="opportunity"
      width="56%"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <div class="basics">基础信息</div>
      <el-form
        :model="opportunityForm"
        ref="opportunityForm"
        :rules="opportunityRules"
        label-position="top"
        size="normal"
      >
        <el-form-item label="商机名称" style="width: 330px" prop="name">
          <el-input
            v-model="opportunityForm.name"
            placeholder="请输入商机名称"
            width="330"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="客户名称" style="width: 330px" prop="definition">
          <el-input v-model="opportunityForm.definition" disabled></el-input>
        </el-form-item>
        <el-form-item label="商机金额" style="width: 330px">
          <el-input
            v-model="opportunityForm.amount"
            placeholder="请输入商机金额"
          ></el-input>
        </el-form-item>
        <el-form-item label="预计成交日期" style="width: 330px">
          <el-date-picker
            v-model="opportunityForm.dealTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
            style="width: 330px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商机状态组" style="width: 330px">
          <el-select
            v-model="opportunityForm.status"
            placeholder="请选择商机状态组"
            style="width: 330px"
          >
            <el-option
              v-for="item in stuOptions"
              :key="item.value + 'one'"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" style="width: 330px">
          <el-input
            v-model="opportunityForm.remark"
            type="textarea"
            placeholder="请输入备注"
            size="normal"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="opportunity = false">取消</el-button>
        <el-button type="primary" @click="opportunitySubmit">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="新建联系人"
      :visible.sync="contactHighShow"
      width="60%"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <div class="basics">基础信息</div>
      <el-form
        :model="contactForm"
        ref="contactForm"
        :rules="contactRules"
        label-position="top"
      >
        <el-form-item label="姓名" style="width: 330px" prop="name">
          <el-input
            v-model="contactForm.name"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户名称" style="width: 330px" prop="definition">
          <el-input v-model="contactForm.definition" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号" style="width: 330px" prop="phone">
          <el-input
            v-model="contactForm.phone"
            placeholder="请输入手机号"
            oninput="value=value.replace(/[^\d]/g,'')"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 330px">
          <el-select
            v-model="contactForm.sex"
            placeholder="请选择联系人性别"
            style="width: 330px"
          >
            <el-option
              v-for="item in sexOptions"
              :key="item.value + 'two'"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职务" style="width: 330px">
          <el-input
            v-model="contactForm.post"
            placeholder="请输入联系人职务"
          ></el-input>
        </el-form-item>
        <el-form-item label="微信" style="width: 330px">
          <el-input
            v-model="contactForm.wechat"
            placeholder="请输入联系人微信"
          ></el-input>
        </el-form-item>
        <el-form-item label="关键联系人" style="width: 330px">
          <el-select
            v-model="contactForm.leader"
            placeholder="请选择关键联系人"
            style="width: 330px"
          >
            <el-option
              v-for="item in cruxOption"
              :key="item.value + 'three'"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" style="width: 330px">
          <el-input
            v-model="contactForm.address"
            placeholder="请输入联系人地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="下次联系时间" style="width: 330px">
          <el-date-picker
            v-model="contactForm.contactTime"
            type="date"
            placeholder="请选择下次联系日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 330px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" style="width: 330px">
          <el-input
            v-model="contactForm.remark"
            placeholder="请输入备注"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="contactHighShow = false">取消</el-button>
        <el-button type="primary" @click="contactSubmit">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="show" :fullscreen="true">
      <div v-html="fileHtml" style="text-align: center"></div>
    </el-dialog>
    <el-dialog
      title="重命名"
      :visible.sync="renameShow"
      width="30%"
      style="margin-top: 200px; margin-left: 600px"
      @close="renameClose"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <el-input
        v-model="input"
        placeholder="请输入文件名"
        size="normal"
      ></el-input>
      <span slot="footer">
        <el-button @click="renameShow = false">取消</el-button>
        <el-button type="primary" @click="rename">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="show" :fullscreen="true">
      <div v-html="fileHtml" style="text-align: center"></div>
    </el-dialog>
  </div>
</template>

<script>
import ghActivity from "./activity.vue";
import {
  getHighSeasActivity,
  addHighSeasContact,
  selectHighSeasContactList,
  addHighSeasOpportunity,
  selectHighSeasOpportunityList,
  getGhCustomerEnclosure,
  delGhCustomerEnclosure,
  previewGhCustomer,
  renameGhCustomer,
  importHighSeasEnclosure,
  OperationRecord,
} from "@/api/CRM";
export default {
  components: { ghActivity },
  props: {
    dialog: {
      type: Object,
      required: true,
    },
    cusShow: {
      type: Number,
      required: true,
    },
    drawerData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      activityForm: {
        type: "",
        contactTime: "",
        content: "",
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      },
      fileHtml: "",
      input: "",
      renameId: "",
      fileHtml: "",
      renameShow: false, //重命名
      show: false, //预览
      furtherData: [],
      contactRules: {
        phone: [
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        definition: [
          {
            required: true,
            message: "请选择客户",
            trigger: "blur",
          },
        ],
      },
      sexOptions: [
        {
          value: "0",
          label: "男",
        },
        {
          value: "1",
          label: "女",
        },
      ],
      cruxOption: [
        {
          value: "1",
          label: "是",
        },
        {
          value: "2",
          label: "否",
        },
      ],
      //商机状态组
      stuOptions: [
        {
          value: "1",
          label: "默认",
        },
      ],
      fileList: [],
      contactForm: {
        //联系人
        name: "",
        definition: this.drawerData.name, //名称
        phone: "",
        sex: "",
        post: "", //职务
        wechat: "",
        leader: "", //关键决策人
        address: "", //地址
        contactTime: "", //下次联系时间
        remark: "", //备注
        crmId: this.drawerData.id,
      },
      opportunityForm: {
        //商机
        definition: this.drawerData.name,
        crmId: this.drawerData.id,
        name: "",
        amount: "",
        dealTime: "",
        status: "",
        remark: "",
      },
      opportunityRules: {
        definition: [
          { required: true, message: "请输入商机名称", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入客户名称", trigger: "blur" }],
      },
      opportunity: false, //新建商机弹窗
      contactHighShow: false, //新建联系人
      opportunityData: [],
      contactData: [],
      enclosureData: [],
      activeName: "first",
      direction: "rtl",
      Operation: [],
    };
  },
  watch: {
    drawerData: {
      deep: true,
      immediate: true,
      handler() {
        if (this.drawerData !== {}) {
          this.getfurther();
          this.getContactList();
          this.getOpportunityList();
          this.getEnclosure();
          this.getOperationList();
        }
      },
    },
  },
  methods: {
    // 操作记录
    getOperationList() {
      OperationRecord({
        columnId: this.drawerData.crmType,
        entryId: this.drawerData.id,
      }).then((res) => {
        this.Operation = res.data;
      });
    },
    handleRemove1(file, fileList) {
      console.log(file, fileList);
    },
    getFile1(file, fileList) {
      this.uploadFile(file);
    },
    uploadFile(file) {
      var fd = new FormData();
      fd.append("crmId", this.drawerData.id);
      fd.append("crmType", this.drawerData.crmType);
      fd.append("file", file.raw);
      importHighSeasEnclosure(fd).then((res) => {
        this.$message.success(res.msg);
        this.getEnclosure();
        this.$refs.uploadFile.clearFiles();
      });
    },
    // 分配
    distributionGh() {
      this.$parent.distributionCus();
      this.$parent.chargeId = this.drawerData.id;
    },
    // 领取
    receive() {
      this.$parent.ids = [];
      this.$parent.ids.push(this.drawerData.id);
      this.$parent.receiveGh();
    },
    // 关闭重命名
    renameClose() {
      this.input = "";
    },
    // 重命名
    renameCus(row) {
      this.renameShow = true;
      this.renameId = row.id;
    },
    // 预览附件
    preview(row) {
      console.log(row);
      previewGhCustomer({ url: row.fileUrl }).then((res) => {
        this.fileHtml = res.msg;
        this.show = true;
      });
    },
    // 新建商机
    opportunitySubmit() {
      this.$refs.opportunityForm.validate((valid) => {
        if (!valid) return this.$message.error("请输入必填项");
        addHighSeasOpportunity(this.opportunityForm).then((res) => {
          this.$message.success(res.msg);
          this.opportunity = false;
          this.getOpportunityList();
        });
      });
    },
    // 查询商机
    getOpportunityList() {
      selectHighSeasOpportunityList({
        crmId: this.drawerData.id,
      }).then((res) => {
        this.opportunityData = res.rows;
      });
    },
    changeStu() {
      this.dialog.statusShow = true;
    },
    // 查询联系人
    getContactList() {
      selectHighSeasContactList({ crmId: this.drawerData.id }).then((res) => {
        this.contactData = res.data;
      });
    },
    contactSubmit() {
      this.$refs.contactForm.validate((valid) => {
        if (!valid) return this.$message.error("请输入必填项");
        addHighSeasContact(this.contactForm).then((res) => {
          this.getContactList();
          this.$message.success(res.msg);
          this.contactHighShow = false;
        });
      });
    },
    edit() {
      this.dialog.newCustomer = true;
      this.dialog.editShow = false;
      this.$emit("updateCus", this.drawerData);
    },
    // 修改名称
    rename() {
      renameGhCustomer({
        id: this.renameId,
        name: this.input,
      }).then((res) => {
        this.$message.success("修改成功");
        this.renameShow = false;
        this.getEnclosure();
        this.input = "";
      });
    },
    // 获取跟进
    getfurther() {
      getHighSeasActivity({
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      }).then((res) => {
        this.furtherData = res.rows;
      });
    },
    // 获取附件列表
    getEnclosure() {
      getGhCustomerEnclosure({
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      }).then((res) => {
        this.enclosureData = res.data;
      });
    },
    // 删除
    del(row) {
      delGhCustomerEnclosure({
        id: row.id,
        url: row.fileUrl,
      }).then((res) => {
        this.$message.success("删除成功");
        this.getEnclosure();
      });
    },
    newContact() {
      this.contactHighShow = true;
    },
    newOpportunity() {
      this.opportunity = true;
    },
    // 关闭抽屉
    drawerClose() {
      this.dialog.eject = false;
      this.dialog.activity = true;
      this.activityForm = {
        type: "",
        contactTime: "",
        content: "",
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      };
    },
  },
};
</script>

<style lang="less" scoped>
// /deep/.el-drawer.rtl {
//   height: 96%;
//   margin-top: 4%;
// }
.head {
  padding: 29px;
  width: 100%;
  height: 155px;
  border-bottom: 1px solid #ececec;
  span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
  .header {
    display: flex;
    justify-content: space-between;
    .img {
      transform: translateY(-15px);
      img {
        vertical-align: middle;
        margin-right: 12px;
      }
      h3 {
        display: inline-block;
      }
    }
  }
  .footer {
    display: flex;
    .information {
      display: flex;
      flex-direction: column;
      margin-right: 80px;
      h4 {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #2b2b2b;
        margin-top: 12px;
      }
    }
  }
}
.strip {
  width: 100%;
  height: 16px;
  background: #fafafa;
  border: 1px solid #ececec;
  /deep/.el-tabs__nav-scroll {
    background-color: #f5f8fa;
  }
}
/deep/.el-tabs__content {
  height: 78vh;
  overflow:auto;
}
/deep/ .el-tab-pane {
  padding-left: 24px;
}
.basics {
  position: relative;
  font-size: 14px;
}
.basics::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}

.system {
  margin-top: 70px;
  position: relative;
  font-size: 14px;
}
.system::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}
.basics-information {
  display: flex;
  flex-wrap: wrap;
  .subclass {
    width: 245px;
    margin-top: 30px;
    margin-right: 150px;
    span {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 14px;
    }
  }
}
.operation {
  display: flex;
  .user {
    display: flex;
    .portrait {
      width: 30px;
      height: 30px;
      border-radius: 30px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 30px;
      }
    }
    .name {
      height: 30px;
      line-height: 30px;
      margin-left: 14px;
    }
  }
  .record {
    padding-left: 21px;
    margin-left: 44px;
    border-left: 1px dotted #dbdcdb;
    .time {
      font-size: 14px;
      position: relative;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 14px;
    }
    .time::before {
      content: "";
      position: absolute;
      left: -27px;
      width: 11px;
      height: 11px;
      background-color: #000;
      transform: rotate(45deg);
    }
    .content {
      padding-top: 20px;
    }
  }
}
.activ {
  width: 100%;
  height: 100%;
}
.cz {
  color: #1267fa;
  margin-left: 5px;
}
.del {
  color: #fa1212;
  margin-left: 5px;
}
.el-form {
  margin-top: 20px;
  margin-left: 50px;
  display: flex;
  flex-wrap: wrap;
  .el-form-item {
    margin-right: 30px;
  }
}
// .specific {
//   height: 4rem;
//   overflow-y: auto;
// }

// /deep/.el-upload-list {
//   display: none;
// }
</style>